package br.com.skytef.servlet.POS;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.skytef.beans.POS.Logica;
import br.com.skytef.beans.POS.SystemSession;
import br.com.skytef.dao.MenuDao;
import br.com.skytef.entity.MenuEntity;
import br.com.skytef.entity.MessageEntity;

public class ServletMenuPrevious implements Logica {
	private MessageEntity sendMessage = new MessageEntity();

	@Override
	public String executa(HttpServletRequest req, HttpServletResponse res,
			HttpSession sess) throws Exception {
		
		SystemSession sysSessionRecover = null;
		SystemSession sysSessionNew = null;

		try {
			sysSessionRecover = (SystemSession) sess.getAttribute("SysSession");
			sysSessionNew = sysSessionRecover;
			
			sendMessage.setTitle("Erro");
			sendMessage.setMessage("Menu anterior");

			
			int menuIndex = sysSessionNew.getAllMenu().size() - 1;
			String menuOrigin = sysSessionNew.getAllMenu().get(menuIndex).get(0).getOrigin();
			
			//Identifica e retorna o menu anterior
			if(menuOrigin.equals("menuMain")){
				
				//Nunca deve cair aqui
				System.out.println("##> Excluir main");
				
			}else if (menuOrigin.toLowerCase().contains("submain")){
				
				sess.setAttribute("MenuSelProduct", "");
				sysSessionNew.setNextServlet("ServletMenuMain");
				sysSessionNew.getAllMenu().get(menuIndex).get(0).setOrigin("menuMain");
				sysSessionNew.setDisplayTitle(sysSessionRecover.getDisplayTitle());
				sess.setAttribute("MenuSelProduct", "");
				sysSessionNew.getAllMenu().remove(menuIndex);
				sysSessionNew.setDisplayTitle("Menu Principal");
				
			}else if (menuOrigin.toLowerCase().contains("category")){
				//sess.setAttribute("MenuSelProduct", menuTypeMenu);
				sess.setAttribute("MenuSelProduct", "Category");
				sysSessionNew.setNextServlet("");
				sysSessionNew.getAllMenu().get(menuIndex).get(0).setOrigin("menuCategory");			
				sysSessionNew.setProductItem(null);
				sysSessionNew.getAllMenu().remove(menuIndex);
				sysSessionNew.setDisplayTitle("Selecione Categoria");
				
			}else if (menuOrigin.toLowerCase().contains("product")){
				sess.setAttribute("MenuSelProduct", "Category");
				sysSessionNew.setNextServlet("ServletItemSelect");
				sysSessionNew.getAllMenu().get(menuIndex).get(0).setOrigin("menuCategory");		
				sysSessionNew.setDisplayTitle(sysSessionRecover.getDisplayTitle());
				sysSessionNew.setProductItem(null);
				sysSessionNew.getAllMenu().remove(menuIndex);
				sysSessionNew.setDisplayTitle("Selecione Categoria");
				
			}else if (menuOrigin.toLowerCase().contains("detail")){
				sess.setAttribute("MenuSelProduct", "Category");
				sysSessionNew.getAllMenu().get(menuIndex).get(0).setOrigin("menuProduct");
				sysSessionNew.setProductItem(null);
				sess.setAttribute("MenuSelProduct", "Product");
				sysSessionNew.getAllMenu().remove(menuIndex);
				sysSessionNew.setDisplayTitle("Selecione Produto");
				
			}else{

				List<MenuEntity> menu0 = new ArrayList<MenuEntity>(); 
				
				MenuDao menuDao = new MenuDao();
				menu0 = menuDao.getMenuMain(0, 1);
				
				sess.setAttribute("MenuSelProduct", "");
				sysSessionNew.setMethodNext("");
				sysSessionNew.setNextServlet("");
				sysSessionNew.setProductItem(null);
				sysSessionNew.setDisplayTitle("Menu Principal");
				sysSessionNew.setMenuAll(new ArrayList<List<MenuEntity>>());
				sysSessionNew.setMenu(menu0);
				
			}		
			
			
			sysSessionNew.setNextView("/jsp/POS/MenuPrincipal.jsp");
						
		} catch (Exception e) {

			sendMessage.setTitle("Erro");
			sendMessage.setMessage(e.getMessage());

			sysSessionNew.setMessage(sendMessage);
			sysSessionNew.setNextServlet("");
			sysSessionNew.setDisplayTitle(sysSessionRecover.getDisplayTitle());
			sysSessionNew.setNextView("/jsp/POS/ModMensagem.jsp");

		}

		sess.setAttribute("SysSession", sysSessionNew);

		return sysSessionNew.getNextView();
	}
}
